using System;
using System.Drawing;
using Clandestine.Engines.Field;

namespace Shlick
{
	/// <summary>
	///  Delegate which given the active level will create an instance of an object (i.e. a new one or selected one in UI).
	/// </summary>
	public delegate Clandestine.Engines.Field.Object CreateObjectDelegate(Level level);

	public class ObjectAddTool : Tool	
	{
		public override string Name { get { return "Add Object"; } }
		public override event ToolCompleteHandler ToolComplete;
		public MapDocument MapDocument { get; private set; } 
		
		private CreateObjectDelegate createObjectDelegate;
		public CreateObjectDelegate CreateObjectDelegate {
			get { return createObjectDelegate; }
			set {
				createObjectDelegate = value;
				this.RecreateObject();
			}
		}

		private Point originalPosition;
		
		private Clandestine.Engines.Field.Object CurrentObject;
		
		public ObjectAddTool(MapDocument mapDocument)
		{
			this.MapDocument = mapDocument;
		}
	
		public override void Activated(params object[] args)
		{
		}
		
        public override void Deactivated()
        {
        	// If we're interrupted, just revert to normal.
        	this.deleteTempObject();
        }
	
        public override void MouseDownLeft(int mouseX, int mouseY, int tileX, int tileY)
        {
       		if ((this.MapDocument.ToolManager.ActiveLevel == null) || (this.CurrentObject == null))
				return;
        
            // Left-click: Complete Add
            
            // Temp object -> Real object!
        	
        	// Command!
        	// todo: use command
        	// for now, as long as we re-create CurrentObject, it's done
        	this.RecreateObject();
        }
        
        public override void MouseUpLeft(int mouseX, int mouseY, int tileX, int tileY)
        {
        }
        
        public override void MouseDownRight(int mouseX, int mouseY, int tileX, int tileY)
        {
        	// Done!
        	this.deleteTempObject(); 
        	if (this.ToolComplete != null)
        		this.ToolComplete();
        }
        
        public override void MouseUpRight(int mouseX, int mouseY, int tileX, int tileY)
        {
        }
        
        public override void MouseMove(int mouseX, int mouseY, int tileX, int tileY)
        {     
       		if ((this.MapDocument.ToolManager.ActiveLevel == null) || (this.CurrentObject == null))
				return;
        
        	// Move object as we move the mouse -- although this move will work out only temporary.
        	if ((this.CurrentObject.Position.X != tileX) || (this.CurrentObject.Position.Y != tileY))
        	{
	        	this.CurrentObject.Position = new Point(tileX, tileY);
	        	this.CurrentObject.Dirty = true;
       		}
        }
        
        public override void LevelChanged (Level oldLevel)
		{
			// Recreate object on the correct level
			this.deleteTempObject();
			this.RecreateObject();
		}
		
		private void RecreateObject()
		{
			if (this.MapDocument.ToolManager.ActiveLevel == null)
				return;
		
			this.CurrentObject = this.CreateObjectDelegate(this.MapDocument.ToolManager.ActiveLevel);
		}
        
        private void deleteTempObject()
        {
        	if (this.CurrentObject != null)
        	{
           		CurrentObject.Level.RemoveObject(CurrentObject);
				CurrentObject.Dispose();
			}
        }
	}
}

